<template>
	<view class="page-box">
		<view class="pad-30">
			<up-swiper @click="hanldeSwiper" interval="1500" v-if="bannerList?.length > 0" :display-multiple-items="1"
				height="300rpx" radius="30rpx" :list="bannerList" keyName="cover" indicator indicatorMode="line"
				circular></up-swiper>
		</view>
		<view class="notice-box bg-white mar-top-30" v-if="notices?.length>0">
			<view class="betweenX">
				<view class="font-w-600 font-30">公告</view>
				<view @click="hanldeNotice" class="centerY font-28">更多 <up-icon size="28rpx"
						name="arrow-right"></up-icon></view>
			</view>
			<view v-for="item in notices" :key="item.id" @click="hanldeNoticeInfo(item.id)" class="mar-top-30 font-28">
				<view>{{item.title}}</view>
				<view class="mar-top-10 border-bottom color-646464">
					{{timeFormat(item.created_at, 'yyyy-mm-dd hh:MM:ss')}}
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import { allOfGroupList, noticeList } from '@/api/index.js'
	import { onShow, onPullDownRefresh,onShareAppMessage } from '@dcloudio/uni-app';
	import { timeFormat } from '@/node_modules/uview-plus';


	const bannerList = ref([]);
	const notices = ref([]);
	const refreshing = ref(false); // 控制刷新状态

	// 轮播跳转
	const hanldeSwiper = () => {
		if (bannerList.value[index].jump_url) {
			uni.navigateTo({
				url: bannerList.value[index].jump_url
			})
		}
	}

	// 跳转公告列表
	const hanldeNotice = () => {
		uni.navigateTo({
			url: '/pages/notice/notice'
		})
	}

	// 跳转公告详情
	const hanldeNoticeInfo = (id) => {
		uni.navigateTo({
			url: '/pages/noticeDetails/noticeDetails?id=' + id
		})
	}

	// 下拉刷新
	onPullDownRefresh(() => {
		getHomeData();
	})

	const getHomeData = async () => {
		try {
			// 获取轮播图
			let banner = await allOfGroupList({ code: 'banner' });

			if (banner.code == 200) {
				bannerList.value = banner.data.lists;
			}

			// 获取公告列表
			let notice = await noticeList({ page: 1, ps: 5 });
			if (notice.code == 200) {
				notices.value = notice.data.data;
			}

			// 停止下拉刷新动画
			uni.stopPullDownRefresh();
		} catch {
			// 停止下拉刷新动画
			uni.stopPullDownRefresh();
		}
	}

	// 页面初始化
	onShow(() => {
		// #ifdef MP-WEIXIN
		wx.hideHomeButton()
		// #endif
		getHomeData();
	})
	
	onShareAppMessage(() => {
		return {
			path: '/pages/home/home',
		};
	})
</script>

<style scoped>
	.notice-box {
		padding: 30rpx;
		box-sizing: border-box;
	}

	.border-bottom {
		border-bottom: 1rpx solid #e2d3d3;
		padding-bottom: 30rpx;
	}
</style>